package com.cckat.controller;

import com.cckat.base.BaseController;
import com.cckat.entity.Community;
import com.cckat.entity.House;
import com.cckat.entity.HouseBroker;
import com.cckat.entity.HouseImage;
import com.cckat.entity.bo.HouseQueryBo;
import com.cckat.entity.vo.HouseVo;
import com.cckat.result.Result;
import com.cckat.service.*;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 静海基地
 */
@RestController
@RequestMapping("/house")
public class HouseController extends BaseController {

    @DubboReference
    private HouseService houseService;

    @DubboReference
    private CommunityService communityService;

    @DubboReference
    private HouseBrokerService houseBrokerService;

    @DubboReference
    private HouseImageService houseImageService;

    @RequestMapping("/list/{pageNum}/{pageSize}")//见前端页面路径
    public Result list(@PathVariable("pageNum") Integer pageNum,
                       @PathVariable("pageSize") Integer pageSize,
                       @RequestBody HouseQueryBo houseQueryBo
                       //此处的@RequestBody注解作用详见SSM整合笔记-9，此处为在SpringMVC中的ajax中为所标注的形参赋值
                       //注意区分@ResponBody，@ResponBody是将java对象转换为json数据，@RequestBody是将json数据转换为map集合或者java对象
    ) {
        //查询房源信息
        PageInfo<HouseVo> page = houseService.findHouseVoPage(pageNum, pageSize, houseQueryBo);
        return Result.ok(page);
    }


    @RequestMapping("/info/{houseId}")
    public Result info(@PathVariable Long houseId) {
        //获取房源信息
        House house = houseService.getById(houseId);
        //获取房源所在小区信息
        Community community = communityService.getById(house.getCommunityId());
        //获取房源的经纪人信息
        List<HouseBroker> houseBrokerList = houseBrokerService.findHouseBrokerListByHouseId(houseId);
        //获取房源图片信息
        List<HouseImage> houseImageList1 = houseImageService.findHouseImageListByHouseIdAndType(houseId, 1);

        Map<String, Object> map = new HashMap<>();
        map.put("house",house);
        map.put("community",community);
        map.put("houseBrokerList",houseBrokerList);
        map.put("houseImageList1",houseImageList1);
        map.put("isFollow",false);

        return Result.ok(map);
    }
}
